$("#form-weeklyInfo-add").validate({
	rules:{
		weeklyName:{
			required:true,
		},
		imageUrl:{
			required:true,
		},
		source:{
			required:true,
		},
		orderBy:{
			required:true,
		}
	},
	submitHandler:function(form){
		addupload();
	}
});


KindEditor.ready(function(K) {
	  	window.editor = K.create('#content', {
	  	uploadJson: ctx + "business/weeklyInfo/uploadFile", 	//文件上传路径
	  	allowFileManager : false,
	  	allowImageRemote: false,
		width : '100%',
		height : '300px'
  	});
});


$(function () {
    $("#imageUrl").change(function (e) {
    	var filepath = $("input[name='imageUrl']").val();
        var extStart = filepath.lastIndexOf(".");
        var ext = filepath.substring(extStart, filepath.length).toLowerCase();
        if (ext != ".bmp" && ext != ".png" && ext != ".gif" && ext != ".jpg" && ext != ".jpeg") {
            alert("图片上传格式不正确,请使用bmp.png.gif.jpeg.jpg格式!");
            return false;
        }
	    var maxSize = 2*1024*1024;    
        var filesize = document.getElementById('imageUrl').files[0].size;
        if(filesize > maxSize){
        	alert('请上传小于2M的图片!')
        	return false;
        }
        var file = e.target.files[0] || e.dataTransfer.files[0];
        $('#photoCover').val(document.getElementById("imageUrl").files[0].name);
        if (file) {
            var reader = new FileReader();
            reader.onload = function () {
                $("img").attr("src", this.result);
            }
            reader.readAsDataURL(file);
        }
        $('#showimg').css("display","block");
    });
})

function addupload(){
	var weeklyName = $('#weeklyName').val();
	var weeklytype = $('#weeklytype').val();
	var articleType = $("#articleType").val();
	var source = $('#source').val();
	var status = $("input[name='status']:checked").val();
	var orderBy = $('#orderBy').val();
	var linkUrl = $('#linkUrl').val();
	var content = editor.html();
	var file = document.getElementById("imageUrl").files[0];
	var formData = new FormData();
    formData.append('weeklyName', weeklyName);
    formData.append('weeklyType', weeklytype);
    formData.append('articleType', articleType);
    formData.append('source', source);
    formData.append('status', status);
    formData.append('orderBy', orderBy);
    formData.append('linkUrl', linkUrl);
    formData.append('content', content);
    formData.append('file', file);
	$.ajax({
		url: ctx + 'business/weeklyInfo/save',
		type: 'post',
		data: formData,
		processData: false,  
        contentType: false,
        success: function(data){
        	handleSuccess(data);
        }
	})
}

function add() {
    _ajax_save(ctx + "business/weeklyInfo/save", $('#form-weeklyInfo-add').serialize());
}